<?php
require_once $_SERVER['DOCUMENT_ROOT'].'/common.php';

$user_id = user::getId();
if (empty($user_id))
{
	sys::gotoUrl('/');
	return false;
}

$item_id = $_GET['item_id'];
$item = item::getById($item_id);
if (empty($item) || $user_id != $item['user_id'])
{
	sys::gotoUrl('/');
	return false;
}

if ($_POST['item_images'])
{
	$posted_item_info = $_POST;

	$item_image_name = array_shift($posted_item_info['item_images']);
	if ($item_image_name)
	{
		$item_image_original = ITEM_IMAGE_UPLOAD_DIR.'/'.$item_image_name;
		if (!file_exists($item_image_original))
		{
			// TODO: should do error flow
			sys::gotoUrl('/');
			return false;
		}
		itemimage::save($posted_item_info['item_id'], $item_image_original);
	}

	$posted_item_info['item_image'] = $item_image_name;
	$posted_item_info['user_id'] = $user_id;
	$posted_item_info['item_id'] = $item_id;
	
	$result = item::edit($posted_item_info);
	$edit_result = $result > 0 ? "修改成功！" : "修改失败！";
	
	sys::gotoUrl('/user/'.$user_id.'/itemtroop/');

}


$item_image = $item['item_image_m'];
$item_emotion = $item['item_emotion'];
page::printTop();
?>

<script type="text/javascript" charset="utf-8">

$(function(){
	$("#button-confirm-upload").hide();

	var im=$("<span><input type=hidden name=item_images[] value=''>"
		+"<img class=upload_image height=200 onerror='this.src=/images/no_image_avail.jpg' src='<?php echo $item_image; ?>' /></span>");
	im.appendTo($('#file-list'));
	$(im).bind('click', function(e){
			$(this).remove();
		});

	var uploader = new qq.FileUploader({
		element:$('#file-uploader')[0],
		params: {},
		allowedExtensions: ['jpg', 'jpeg', 'png', 'gif', 'bmp'],        
		debug: false,
		onSubmit: function(id, fileName){},
		onProgress: function(id, fileName, loaded, total){},
		onComplete: function(id, fileName, responseJSON){
			if (!responseJSON.success) return false;
			$('#file-list').html('');
			
			$("<input type=hidden name=item_images[] value='"+responseJSON.upname+"'>").appendTo($('#file-list'));
			var im=$("<span>"
				+"<img class=upload_image height=200 src='<?php echo ITEM_IMAGE_UPLOAD_BASE_URL."/"; ?>"+responseJSON.upname+"' /></span>");
			im.appendTo($('#file-list'));
			$(im).bind('click', function(e){
					$(this).remove();
				});
			$('#button-confirm-upload').show();
			
			$('.qq-upload-list').hide();
		},
		onCancel: function(id, fileName){},
		messages: {
		},
		showMessage: function(message){ alert(message); }
	});
});
</script>


<div style="text-align:center">（* 为必填项） <?php echo '<font color=red>'.$add_result.'</font>'; ?></div><hr />

<form name="upform" method=post enctype="multipart/form-data">
	<div style="margin-left:200px;margin-top:50px;">
		<div style="float:left;">[*] 请上传图：</div>
		<div>
			<div id="file-uploader">
				<noscript>          
				<p>Please enable JavaScript to use file uploader.</p>
				</noscript>         
			</div>
		</div>
	</div>
	<div id="file-list" style="margin-left:268px;padding-top:10px;"></div>
	<div style="margin-left:200px;">
		<div style="float:left;">图片文字：</div>
		<div>
			<input value="<?php echo $item_emotion; ?>" name="item_emotion" maxlength=200 style="width:400px" />
		</div>
	</div>
	<div style="float:none;margin-left:268px;margin-top:30px;">
		<input id="button-confirm-upload" style="width:200px;" type="submit" value="修  改" />
	</div>

</form>

<?php
page::printTail();
